package com.ffapp.rainmonitorcustom.bean;


import android.content.Context;
import android.text.TextUtils;

import com.ffapp.rainmonitorcustom.basis.Constants;
import com.ffapp.rainmonitorcustom.jpush.TagAliasOperatorHelper;

import my.FileUtils;
import my.LogUtil;
import my.MySharedPreferences;


public class LoginBean extends BasisBean implements Cloneable {
    private static final long serialVersionUID = 1L;

    public static final int ERRO_CODE_DEVICE = -3001;


    public static final int LOGIN_TYPE_PERSON = 0;
    public static final int LOGIN_TYPE_FA = 1;

    public static final String FILE_CACHE = Constants.DIR_FILECACHE
            + "login.data";
    private static LoginBean sLoginBean;

    private String accToken;
    private String jpushAlias;
    private String dvrVideoHost;
    private String dvrVideoPort;
    private String rtmpPort;
    private String rtmpUrl;

    private ResultUser resultUser;

    private boolean ywryAddRight;
    private boolean sjglryAddRight;


    public LoginBean() {
        super();
        // sLoginBean = this;
    }

    public static boolean isLogin() {
		return getInstance() != null && !TextUtils.isEmpty(sLoginBean.getAccToken());
//        return getInstance() != null && getInstance().getUser() != null;
//        return false;
    }

    public static LoginBean getInstance() {
        if (sLoginBean == null)
            sLoginBean = getFromCache();
        return sLoginBean;
    }

    public static LoginBean getFromCache() {
        LoginBean data;
        data = (LoginBean) FileUtils.readObject(FILE_CACHE);
        if (data != null) {
            sLoginBean = data;
        }
        LogUtil.i("login---->getFromCache---" + (data == null));
        return data;
    }

    public void save() {
        sLoginBean = this;
        FileUtils.cacheObject(FILE_CACHE, this);
        // FileSaveHandler.saveObject(FILE_CACHE, this);
        LogUtil.i("login---->save---" + FILE_CACHE);
    }

    private static void delete() {
        FileUtils.cacheObject(FILE_CACHE, null);
        // FileSaveHandler.removeObject(FILE_CACHE);
    }

	public static void logout() {
		delete();
		sLoginBean = null;

        TagAliasOperatorHelper.getInstance().cleanAlias();
	}



    public static void logout(Context mContext) {
        delete();
        sLoginBean = null;
        MySharedPreferences mSp = new MySharedPreferences(mContext);
        mSp.putIsLogined(false);
        mSp.putPassword("");
        mSp.putUser("");
        TagAliasOperatorHelper.getInstance().cleanAlias();
    }

    public static void logPast(Context mContext) {
        delete();
        sLoginBean = null;
        MySharedPreferences mSp = new MySharedPreferences(mContext);
        mSp.putIsLogined(false);
    }

    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }


//    public   String getJpushAlias(){
//        return "ukluser_"+getUserId();
//    }

    public static String getUserToken() {
        if (!isLogin()) return "";
        return getInstance().getAccToken();
//        return getInstance().getUser().getUserId();
    }


    public String getJpushAlias() {
        return jpushAlias;
    }

    public void setJpushAlias(String jpushAlias) {
        this.jpushAlias = jpushAlias;
    }


    public ResultUser getResultUser() {
        return resultUser;
    }

    public void setResultUser(ResultUser resultUser) {
        this.resultUser = resultUser;
    }


    public String getAccToken() {
        return accToken;
    }

    public void setAccToken(String accToken) {
        this.accToken = accToken;
    }

    public String getDvrVideoHost() {
        return dvrVideoHost;
    }

    public void setDvrVideoHost(String dvrVideoHost) {
        this.dvrVideoHost = dvrVideoHost;
    }

    public String getDvrVideoPort() {
        return dvrVideoPort;
    }

    public void setDvrVideoPort(String dvrVideoPort) {
        this.dvrVideoPort = dvrVideoPort;
    }

    public boolean isYwryAddRight() {
        return ywryAddRight;
    }

    public void setYwryAddRight(boolean ywryAddRight) {
        this.ywryAddRight = ywryAddRight;
    }

    public boolean isSjglryAddRight() {
        return sjglryAddRight;
    }

    public void setSjglryAddRight(boolean sjglryAddRight) {
        this.sjglryAddRight = sjglryAddRight;
    }

    public String getRtmpPort() {
        return rtmpPort;
    }

    public void setRtmpPort(String rtmpPort) {
        this.rtmpPort = rtmpPort;
    }

    public String getRtmpUrl() {
        return rtmpUrl;
    }

    public void setRtmpUrl(String rtmpUrl) {
        this.rtmpUrl = rtmpUrl;
    }
}
